package com.gz.mylife.app;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.gz.mylife.R;
import com.gz.mylife.widget.LoadingCover;
import com.gz.mylife.widget.LoadingCover.OnLoadingCoverRefreshListener;

public class DeliveryExplain extends BaseActivity implements OnLoadingCoverRefreshListener {

	private LoadingCover mLoadingCover;
	private WebView mWebView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_delivery_explain);
//		mActionBar.setTitle("配送说明");
		setActionbarTitle("配送说明", false);
		mLoadingCover = (LoadingCover) findViewById(R.id.layout_loading_cover);
		mLoadingCover.setOnLoadingCoverRefreshListener(this);
		
		mWebView = (WebView) findViewById(R.id.webview);
		mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.loadUrl(getIntent().getStringExtra("url"));
		mWebView.setWebViewClient(new WebViewClient() {
			
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				mLoadingCover.onRefreshSuccess();
			}
			
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
				super.onReceivedError(view, errorCode, description, failingUrl);
				mLoadingCover.onRefreshFailed();
			}
			
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				//调用拨号程序  
                if (url.startsWith("mailto:") || url.startsWith("geo:") ||url.startsWith("tel:")) {  
                	Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));  
                	startActivity(intent); 
                	return true;
                }
                
				return super.shouldOverrideUrlLoading(view, url);
//                view.loadUrl(url);
//                return true;
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.delivery_explain, menu);
//		MenuItemCompat.setShowAsAction(menu.findItem(R.id.action_quick_navigate),
//				MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			if(mWebView.canGoBack()) {
				mWebView.goBack();
				return true;
			}
			finish();
			return true;
			
		default:
			return super.onOptionsItemSelected(item);
		}
		
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mWebView.destroy();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
			mWebView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onLoadingCoverRefresh() {
		// TODO Auto-generated method stub
		mWebView.loadUrl(getIntent().getStringExtra("url"));
	}

}
